package apiWeb

import (
	liveResource "ZHONGYIHANGYAN/business/liveResource"
	"fmt"

	"ZHONGYIHANGYAN/web"

	requset "ZHONGYIHANGYAN/bean/request"

	"github.com/gin-gonic/gin"
)

func (api *Api) LiveResourceRouters(g *gin.RouterGroup) {
	routerG := g.Group("/liveResource")
	{
		// 获取直播资源列表
		routerG.POST("/getLiveResourceList", web.NewWebRouter(api.getLiveResourceList).Func)
		// 添加或修改直播资源
		routerG.POST("/addOrUpdateLiveResource", web.NewWebRouter(api.addOrUpdateLiveResource).Func)
		// 删除直播资源
		routerG.POST("/deleteLiveResource", web.NewWebRouter(api.deleteLiveResource).Func)
		// 获取直播资源详情
		routerG.POST("/getLiveResourceDetail", web.NewWebRouter(api.getLiveResourceDetail).Func)
	}
}

func (api *Api) InitLiveResourceRouter() error {
	api.liveResourceManage = liveResource.Manage()
	api.web.RegRouters(api.LiveResourceRouters)
	return nil
}

// 获取直播资源列表
func (api *Api) getLiveResourceList(c *gin.Context, replayData *interface{}) error {
	query := requset.GetLiveResourceListQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	infoList, err := api.liveResourceManage.GetLiveResourceList(&query)
	if err != nil {
		return err
	}
	*replayData = infoList
	return nil
}

// 添加或修改直播资源
func (api *Api) addOrUpdateLiveResource(c *gin.Context, replayData *interface{}) error {
	info := requset.AddOrUpdateLiveResourceQuery{}
	if err := api.web.GetPayload(c, &info); err != nil {
		return err
	}
	if err := api.liveResourceManage.AddOrUpdateLiveResource(&info); err != nil {
		return err
	}
	return nil
}

// 删除直播资源
func (api *Api) deleteLiveResource(c *gin.Context, replayData *interface{}) error {
	query := requset.DeleteLiveResourceQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	if len(query.LiveResourceIds) == 0 {
		return fmt.Errorf("请传入需要删除的直播资源id")
	}
	if err := api.liveResourceManage.DeleteLiveResource(query.LiveResourceIds); err != nil {
		return err
	}
	return nil
}

// 获取直播资源详情
func (api *Api) getLiveResourceDetail(c *gin.Context, replayData *interface{}) error {
	query := requset.GetLiveResourceDetailQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	info, err := api.liveResourceManage.GetLiveResourceDetail(&query)
	if err != nil {
		return err
	}
	*replayData = info
	return nil
}
